package v1

import (
	"daily-blog/model"
	"daily-blog/pkg"
	"daily-blog/service"
	"github.com/gin-gonic/gin"
)

func CreateCategory(c *gin.Context) {
	categoryName := c.PostForm("category_name")
	categoryDesc := c.PostForm("category_desc")
	categoryImage := c.PostForm("category_image")
	if categoryName == "" || categoryDesc == "" || categoryImage == "" {
		pkg.FailWithMessage("分类名为空或描述或图片为空", c)
		return
	}
	// 生成id
	categoryID := pkg.GenUUID()
	category := &model.Category{Name: categoryName, CategoryId: categoryID, CategoryDesc: categoryDesc, CategoryImage: categoryImage}
	// 创建
	if ok := service.CreateCategory(category); !ok {
		pkg.Fail(c)
		return
	}
	pkg.Ok(c)
}

func DeleteCategory(c *gin.Context) {
	categoryId := c.Query("category_id")
	if categoryId == "" {
		pkg.FailWithMessage("标签id为空", c)
		return
	}
	if ok := service.DeleteCategoryById(categoryId); !ok {
		pkg.Fail(c)
		return
	}
	pkg.Ok(c)
}

func GetCategoryList(c *gin.Context) {
	categoryList, ok := service.GetCategoryList()
	if !ok {
		pkg.Fail(c)
		return
	}
	pkg.OkWithData(categoryList, c)
}

func UpdateCategory(c *gin.Context) {
	categoryId := c.PostForm("category_id")
	if categoryId == "" {
		pkg.FailWithMessage("目录id为空", c)
		return
	}
	categoryName := c.PostForm("category_name")
	if categoryName == "" {
		pkg.FailWithMessage("目录名为空", c)
		return
	}
	categoryDesc := c.PostForm("category_desc")
	if categoryDesc == "" {
		pkg.FailWithMessage("目录描述为空", c)
		return
	}
	categoryImage := c.PostForm("category_image")
	if categoryImage == "" {
		pkg.FailWithMessage("目录图片为空", c)
		return
	}
	if Ok := service.UpdateCategory(categoryId, categoryName, categoryDesc, categoryImage); !Ok {
		pkg.Fail(c)
		return
	}
	pkg.Ok(c)
}
